﻿/*
 * 切换科室
 * */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WH.NurseSys.Model;
using WH.NurseSys.BLL;

namespace WH.NurseSys.Report
{
    public partial class TranslateDeptForm : Form
    {
        Patient patient;

        public TranslateDeptForm(Patient patient)
        {
            InitializeComponent();
            this.patient = patient;
        }

        private void TranslateDeptForm_Load(object sender, EventArgs e)
        {
            textBoxName.Text = patient.Name;
            textBoxSourceDept.Text = patient.DeptName;
            if (Global.deptList != null)
            {
                comboBoxDestDept.DataSource = Global.deptList;
                comboBoxDestDept.DisplayMember = "Name";
                comboBoxDestDept.ValueMember = "Id";
            }
            else
            {
                buttonRefreshDept.Enabled = false;
                toolStripProgressBar.Style = ProgressBarStyle.Marquee;
                toolStripStatusLabelMsg.Text = "正在获取科室列表";
                backgroundWorkerDept.RunWorkerAsync();
            }
        }

        //异步获取科室列表
        private void backgroundWorkerDept_DoWork(object sender, DoWorkEventArgs e)
        {
            List<Dept> deptList = new DeptBLL().GetList();
            e.Result = deptList;
        }

        //完成异步获取科室列表
        private void backgroundWorkerDept_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            buttonRefreshDept.Enabled = true;
            toolStripProgressBar.Style = ProgressBarStyle.Continuous;
            toolStripStatusLabelMsg.Text = "获取科室列表完成";
            Global.deptList = e.Result as List<Dept>;
            comboBoxDestDept.DataSource = Global.deptList;
            comboBoxDestDept.DisplayMember = "Name";
            comboBoxDestDept.ValueMember = "Id";
        }

        //刷新候选科室列表
        private void buttonRefreshDept_Click(object sender, EventArgs e)
        {
            if (!backgroundWorkerDept.IsBusy)
            {
                buttonRefreshDept.Enabled = false;
                toolStripProgressBar.Style = ProgressBarStyle.Marquee;
                toolStripStatusLabelMsg.Text = "正在获取科室列表";
                backgroundWorkerDept.RunWorkerAsync();
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        //保存病人信息
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!backgroundWorkerPatient.IsBusy)
            {
                buttonOK.Enabled = false;
                toolStripProgressBar.Style = ProgressBarStyle.Marquee;
                toolStripStatusLabelMsg.Text = "正在保存病人信息";
                patient.DeptId = (Guid)comboBoxDestDept.SelectedValue;               
                backgroundWorkerPatient.RunWorkerAsync(patient);
            }
        }

        //异步保存病人信息
        private void backgroundWorkerPatient_DoWork(object sender, DoWorkEventArgs e)
        {
            Patient patient = e.Argument as Patient;
            e.Result = new PatientBLL().Update(patient);
        }

        //完成异步保存病人信息
        private void backgroundWorkerPatient_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            buttonOK.Enabled = true;
            toolStripProgressBar.Style = ProgressBarStyle.Continuous;
            if ((bool)e.Result)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                toolStripStatusLabelMsg.Text = "保存病人信息失败";
            }
        }
    }
}
